﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_TaiKhoan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GlobalFunctions gf = new GlobalFunctions();
        if (gf.IsLogin() != 1) {
            Response.Redirect("~/admin");
        }
        if (!this.IsPostBack)
        {
            //load data for Tel
            SimDSTableAdapters.tUserTableAdapter ad = new SimDSTableAdapters.tUserTableAdapter();
            SimDS.tUserDataTable tb = ad.GetData();
            txtSoDienThoai.Text = tb[0].Tel.ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e) {// change password
        string user_oldPass = txtPass.Text;
        string passwordmoi = txtNewPass.Text;
        string passwordmoinhaplai = txtRetype.Text;
        if (passwordmoi != passwordmoinhaplai) {
            Response.Write("<script>alert('Mật khẩu không khớp!')</script>");
            return;
        }
        SimDSTableAdapters.tUserTableAdapter tkadapter = new SimDSTableAdapters.tUserTableAdapter();
        SimDS.tUserDataTable tktable = tkadapter.GetData();
        string DB_HashPass = tktable[0].Hash;
        string DB_Salt = tktable[0].UID;
        if (GlobalFunctions.verifyPassword(user_oldPass, DB_HashPass, DB_Salt) == false) {
            //sai mat khau cu, bao loi
            Response.Write("<script>alert('Sai mật khẩu!')</script>");
            return;
        }      
        
        string chuoigiatrihash = GlobalFunctions.createHash(passwordmoi, "Admin");

        tkadapter.UpdatePass(chuoigiatrihash);
        Response.Write("<script>alert('Thành công!')</script>");

    }
    protected void DoiDienThoai_Click(object sender, EventArgs e) {
        SimDSTableAdapters.tUserTableAdapter ad = new SimDSTableAdapters.tUserTableAdapter();
        ad.updateTel(txtSoDienThoai.Text);
        Response.Write("<script>alert('Cập nhật thành công!')</script>");
        //Response.Redirect("~/admin/TaiKhoan.aspx");
    }
    protected void btnThemYahoo_Click(object sender, EventArgs e) {
        if (txtYahooID.Text == "")
            return;

        SimDSTableAdapters.tYahooContactTableAdapter ad = new SimDSTableAdapters.tYahooContactTableAdapter();
        ad.Insert(txtYahooID.Text, txtYahooTel.Text);        

        Response.Redirect("~/admin/TaiKhoan.aspx");
    }
    
}
